<template>
  <el-dialog
    :before-close="handleClose"
    :close-on-click-modal="false"
    :title="title"
    :visible.sync="dialogFormVisible"
    width="909px"
  >
    <div class="upload">
      <el-alert
        :closable="false"
        :title="`支持mp4/ogg/webm格式,视频时长不可大于${size}Min`"
        type="info"
      ></el-alert>
      <br />
      <el-upload
        ref="upload"
        :action="action"
        :auto-upload="false"
        :close-on-click-modal="false"
        :data="data"
        :file-list="multipartFile"
        :headers="headers"
        :limit="limit"
        :multiple="false"
        name="multipartFile"
        :on-error="handleError"
        :on-exceed="handleExceed"
        :on-progress="handleProgress"
        :on-remove="handleRemove"
        :on-success="handleSuccess"
      >
        <el-button size="small" type="primary">点击选择本地视频</el-button>
      </el-upload>
    </div>
    <div
      slot="footer"
      class="dialog-footer"
      style="position: relative; padding-right: 15px; text-align: right"
    >
      <div
        v-if="show"
        style="position: absolute; top: 10px; left: 15px; color: #999"
      >
        正在上传中...
      </div>
      <el-button type="primary" @click="handleClose">关闭</el-button>
      <el-button
        :loading="loading"
        size="small"
        style="margin-left: 10px"
        type="success"
        @click="submitUpload"
      >
        开始上传
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
  export default {
    name: 'VabUpload',
    props: {
      url: {
        type: String,
        default: '/upload',
        required: true,
      },
      name: {
        type: String,
        default: 'multipartFile',
        required: true,
      },
      limit: {
        type: Number,
        default: 1,
        required: true,
      },
      size: {
        type: Number,
        default: 50,
        required: true,
      },
      torefresh: {
        type: Function,
        default: null,
      },
    },
    data() {
      return {
        show: false,
        loading: false,
        dialogVisible: false,
        action: 'http://10.170.146.175:8080/cvsm/video/upload',
        headers: {},
        multipartFile: [],
        imgNum: 0,
        imgSuccessNum: 0,
        imgErrorNum: 0,
        typeList: null,
        title: '上传',
        dialogFormVisible: false,
        data: {},
      }
    },
    computed: {
      percentage() {
        if (this.allImgNum === 0) return 0
        return this.$baseLodash.round(this.imgNum / this.allImgNum, 2) * 100
      },
    },
    methods: {
      submitUpload() {
        this.$refs.upload.submit()
      },
      handleProgress(event, file, multipartFile) {
        this.loading = true
        this.show = true
      },
      handleSuccess(response, file, multipartFile) {
        this.imgNum = this.imgNum + 1
        this.imgSuccessNum = this.imgSuccessNum + 1
        if (multipartFile.length === this.imgNum) {
          setTimeout(() => {
            this.$baseMessage(`上传完成,快去看看吧!`, 'success')
          }, 1000)
        }
        setTimeout(() => {
          this.loading = false
          this.show = false
        }, 1000)
        this.dialogFormVisible = false
        this.$props.torefresh()
      },
      handleError(err, file, multipartFile) {
        this.imgNum = this.imgNum + 1
        this.imgErrorNum = this.imgErrorNum + 1
        this.$baseMessage(`上传失败!`, 'error')
        setTimeout(() => {
          this.loading = false
          this.show = false
        }, 1000)
      },
      handleRemove(file, multipartFile) {
        this.imgNum = this.imgNum - 1
        this.allNum = this.allNum - 1
      },
      handleExceed(files, multipartFile) {
        this.$baseMessage(
          `当前限制选择 ${this.limit} 个文件，本次选择了
             ${files.length}
             个文件`,
          'error'
        )
      },
      handleShow(data) {
        this.title = '上传'
        this.data = data
        this.dialogFormVisible = true
      },
      handleClose() {
        this.multipartFile = []
        this.allImgNum = 0
        this.imgNum = 0
        this.imgSuccessNum = 0
        this.imgErrorNum = 0
        this.dialogFormVisible = false
      },
    },
  }
</script>

<style lang="scss" scoped>
  .upload {
    height: 200px;

    .upload-content {
      .el-upload__tip {
        display: block;
        height: 30px;
        line-height: 30px;
      }

      ::v-deep {
        .el-upload--picture-card {
          width: 128px;
          height: 128px;
          margin: 3px 8px 8px 8px;
          border: 2px dashed #c0ccda;
        }

        .el-upload-list--picture {
          margin-bottom: 20px;
        }

        .el-upload-list--picture-card {
          .el-upload-list__item {
            width: 128px;
            height: 128px;
            margin: 3px 8px 8px 8px;
          }
        }
      }
    }
  }
</style>
